package inglar.autotc.client.ui;

import inglar.autotc.client.Utilities;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Button;

public class DialogBoxAddClient extends DialogBox {
	private VerticalPanel contentPanel;
	private TextBox txtbxIpaddress;
	private IntegerBox intbxPort;
	private Button btnAdd;
	private Button btnCancel;

	public DialogBoxAddClient() {
		setHTML("Add Client");
		
		contentPanel = new VerticalPanel();
		contentPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		contentPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		setWidget(contentPanel);
		contentPanel.setSize("100%", "100%");
		
		FlexTable flexTable = new FlexTable();
		contentPanel.add(flexTable);
		
		Label lblIpAddress = new Label("IP Address");
		flexTable.setWidget(0, 0, lblIpAddress);
		
		txtbxIpaddress = new TextBox();
		flexTable.setWidget(0, 1, txtbxIpaddress);
		
		Label lblClientPort = new Label("Client Port");
		flexTable.setWidget(1, 0, lblClientPort);
		
		intbxPort = new IntegerBox();
		intbxPort.setVisibleLength(6);
		flexTable.setWidget(1, 1, intbxPort);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		contentPanel.add(horizontalPanel);
		
		btnAdd = new Button("Add");
		horizontalPanel.add(btnAdd);
		btnAdd.getElement().setAttribute("style", "margin-left: 5px; margin-right: 5px;");
		btnAdd.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String ipaddr = txtbxIpaddress.getText().trim();
				Integer port = intbxPort.getValue();
				
				Utilities.showLoadingModal();
				
				addClient(ipaddr, port);
			}
		});
		
		btnCancel = new Button("Cancel");
		horizontalPanel.add(btnCancel);
		btnCancel.getElement().setAttribute("style", "margin-left: 5px; margin-right: 5px;");
		btnCancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide();
			}
		});
	}

	protected void addClient(String ipaddr, Integer port) {
		String msg = "";
		
		if(ipaddr == null || ipaddr.isEmpty()) {
			msg += "IP Address in invalid. \n";
		}
		if(port == null) {
			msg += "Port number is invalid. \n";
		} else if(port.intValue() == 0) {
			msg += "Port number is invalid. \n";
		}
		if(msg.isEmpty()) {
			
			AsyncCallback<String> addClientCallback = new AsyncCallback<String>() {
				@Override
				public void onSuccess(String result) {
					if(!result.trim().isEmpty()) {
						Window.alert("Unable to add client.\n\n" + result);
					}
					Utilities.hideLoadingModal();
					hide();
					Location.reload();
				}
				@Override
				public void onFailure(Throwable caught) {
					Utilities.failureAlert("addClientCallback", "RPC failure", caught);
					Utilities.hideLoadingModal();
					hide();
					Location.reload();
				}
			};
			
			String userid = Cookies.getCookie("QID");
			Utilities.rpc.setClient(userid, ipaddr, port, addClientCallback);
			
		} else {
			Window.alert(msg);
			Utilities.hideLoadingModal();
		}
	}

}
